<template>
  <view
    class="m-nav-bar"
    :class="mode === 'title' ? 'mode__title' : 'mode__input'"
    :style="containerStyle"
  >
    <CSafeArea :top="true"></CSafeArea>
    <view class="nav-bar-ctnr">
      <view class="left-actions">
        <IconButton
          v-if="backButton && mode === 'input'"
          class="back-button"
          color="#000"
          :icon="'\ue82f'"
          font-size="36rpx"
          @click="back"
        ></IconButton>
        <view v-else class="spacer"></view>
      </view>
      <view class="center-ctnr">
        <text v-if="mode === 'title'" class="title text-ellipsis">{{
          title
        }}</text>
        <view v-if="mode === 'input'" class="input-bar" @click="onInputClick">
          <text class="iconfont icon">&#xe671;</text>
          <text class="placeholder">{{ inputPlaceholder }}</text>
        </view>
      </view>
      <IconButton
        v-if="backButton && mode === 'title'"
        class="back-button"
        color="#000"
        :icon="'\ue82f'"
        font-size="36rpx"
        @click="back"
      ></IconButton>
      <view class="right-actions">
        <slot name="actions">
          <view class="spacer"></view>
        </slot>
      </view>
    </view>
  </view>
</template>

<script>
import CSafeArea from '@/components/c-safe-area/c-safe-area';
import IconButton from '../c-icon-button/c-icon-button.vue';

export default {
  name: 'NavBar',
  components: { IconButton, CSafeArea },
  props: {
    mode: {
      type: String,
      default: 'title',
    },
    backgroundColor: {
      type: String,
      default: 'white',
    },
    fixed: {
      type: Boolean,
      default: false,
    },
    footprint: {
      type: Boolean,
      default: true,
    },
    title: {
      type: String,
      default: '',
    },
    backButton: {
      type: Boolean,
      default: false,
    },
    inputPlaceholder: {
      type: String,
      default: '',
    },
    bottomBorderColor: {
      type: String,
      default: 'transparent',
    },
  },
  computed: {
    containerStyle() {
      return `background-color:${this.backgroundColor};border-bottom:1rpx solid ${this.bottomBorderColor}`;
    },
  },
  methods: {
    back() {
      // uni.navigateBack({
      //   delta: 1
      // });
      uni.navigateBack();
    },
    onInputClick() {
      this.$emit('clickInput');
    },
  },
};
</script>

<style lang="scss">
$contentHeight: 82rpx;
.m-nav-bar {
  width: 750rpx;
  .status-bar {
  }
  .nav-bar-ctnr {
    position: relative;
    display: flex;
    flex-direction: row;
    flex-wrap: nowrap;
    align-items: center;
    justify-content: center;
    .left-actions,
    .right-actions {
      .spacer {
        width: 40rpx;
      }
    }
    .back-button {
      width: 100rpx;
    }
    .center-ctnr {
      flex: 1;
      display: flex;
      flex-direction: row;
      justify-content: center;
      height: $contentHeight;
      .title {
        width: 450rpx;

        text-align: center;
        font-size: 34rpx;
        line-height: $contentHeight;
        font-weight: bold;
      }
      .input-bar {
        display: flex;
        flex-direction: row;
        align-items: center;
        padding: 0 8rpx;
        background-color: #f5f5f5;
        border-radius: 10rpx;
        .icon {
          width: 60rpx;
          text-align: center;
          font-size: 34rpx;
          line-height: 72rpx;
          color: $second-color;
        }
        .placeholder {
          font-size: 28rpx;
          line-height: 72rpx;
          color: #999999;
        }
      }
    }
  }
  &.mode__title {
    .nav-bar-ctnr {
      position: relative;
      .left-actions,
      .right-actions {
        position: absolute;
      }
      .left-actions {
        left: 0;
      }
      .right-actions {
        right: 0;
      }
      .back-button {
        position: absolute;
        left: 0;
      }
      .center-ctnr {
      }
    }
  }
}
</style>
